import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.input.SAXBuilder;

public class JDomTest {

	public static void main(String[] args) {
		try {
			// 创建一个SAXBuilder对象
			SAXBuilder sb = new SAXBuilder();
			File file = new File("C:\\Users\\Administrator\\Desktop\\xmlchat\\xml-chat\\src\\test\\resources\\1.xml");
			FileInputStream in = new FileInputStream(file);
			// 构造文档对象
			Document doc = sb.build(in);
			// 获取根元素bookstore
			Element root = doc.getRootElement();
			// 取名字为book的所有元素
			List<Element> books = root.getChildren("book");
			for (int i = 0; i < books.size(); i++) {
				Element e = books.get(i);
				List<Element> sube = e.getChildren();
				System.out.println("第" + (i + 1) + "本书信息：");
				e = sube.get(0);
				System.out.println(e.getName() + "=" + e.getText());
				e = sube.get(1);
				System.out.println(e.getName() + "=" + e.getText());
				e = sube.get(2);
				System.out.println(e.getName() + "=" + e.getText());

			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
